<?php

namespace App\Models;

use App\Util\Url;
use Illuminate\Database\Eloquent\Model;

class Memos extends Model
{
    protected $table = 'c_memos';

    /****
     * @param $data
     * @return bool
     * 添加备忘录
     */
    public function doSetting($data)
    {
        if (isset($data['id']) && !empty($data['id'])) {
            $redata = $this->getInfo($data['id']);
            $redata->case_id = isset($data['case_id']) ? $data['case_id'] : $redata->case_id;
            $redata->content = isset($data['content']) ? $data['content'] : $redata->content;
            $redata->image = isset($data['image']) ? $data['image'] : $redata->image;
            $redata->voice = isset($data['voice']) ? $data['voice'] : $redata->voice;
            $redata->ins_times = isset($data['ins_times']) ? $data['ins_times'] : $redata->ins_times;
            $redata->user_id = isset($data['user_id']) ? $data['user_id'] : $redata->user_id;
//            $redata->type = isset($data['type']) ? $data['type'] : $redata->type;
            $redata->times = isset($data['times']) ? $data['times'] : $redata->times;
            $res = $redata->save();
        } else {
            //添加
            $this->case_id = isset($data['case_id']) ? $data['case_id'] : 0;
            $this->image = isset($data['image']) ? $data['image'] : 0;
            $this->voice = isset($data['voice']) ? $data['voice'] : 0;
            $this->content = $data['content'];
            $this->ins_times = $data['ins_times'];
            $this->user_id = $data['user_id'];
//            $this->type = $data['type'];
            $this->times = $data['times'];
            $res = $this->save();
        }
//        (new DoctorWorks())->addDefaultTargets();
        return $res;
    }

    /***
     * @param $data
     * @return mixed
     * 获取备忘录信息
     */
    public function getInfo($id)
    {
        return $this->where([['id', '=', $id]])->first();
    }

    /****
     * @param $data
     * 获取备忘录列表
     */
    public function getMemoList($param)
    {
        $where = [];
        if (isset($param['start']) && !empty($param['start'])) {  //开始时间
            $where[] = ['ins_times', '>=', $param['start']];
            $or[] = ['ins_times', '>=', $param['start']];
        }
        if (isset($param['end']) && !empty($param['end'])) {  //开始时间
            $where[] = ['ins_times', '<=', $param['end']];
            $or[] = ['ins_times', '<=', $param['end']];
        }
        if (isset($param['case_id']) && !empty($param['case_id'])) {  //病例id
            $where[] = ['case_id', '=', $param['case_id']];
        }
        if (isset($param['user_id']) && !empty($param['user_id'])) {  //用户id
            $where[] = ['user_id', '=', $param['user_id']];
        }
        $or[] = ["case_id",'=',0];
        $or[] = ["user_id",'=',$param['user_id']];
        $list = $this->where($where)->orWhere($or)->orderByDesc("ins_times")->orderByDesc("created_at")->get();
        if (!empty($list)) {
            $list = $list->toArray();
            $url =  new Url();
            foreach ($list as $k => $v) {
                if (!empty($v['image'])) {
                    $list[$k]['image'] = $url::getHttpHost() . 'upload/' . $v['image'];
                }
                if (!empty($v['voice'])) {
                    $list[$k]['voice'] = $url::getHttpHost() . 'upload/' . $v['voice'];
                }
            }
            $list = self::array_group_by($list, 'ins_times');
            return $list;
        }
        return [];
    }

    /**
     * @param $param
     * @return array
     * android使用
     */
    public function getMemoListAndroid($param)
    {
        $where = [];
        if (isset($param['start']) && !empty($param['start'])) {  //开始时间
            $where[] = ['ins_times', '>=', $param['start']];
            $or[] = ['ins_times', '>=', $param['start']];
        }
        if (isset($param['end']) && !empty($param['end'])) {  //开始时间
            $where[] = ['ins_times', '<=', $param['end']];
            $or[] = ['ins_times', '<=', $param['end']];
        }
        if (isset($param['case_id']) && !empty($param['case_id'])) {  //病例id
            $where[] = ['case_id', '=', $param['case_id']];
        }
        if (isset($param['user_id']) && !empty($param['user_id'])) {  //用户id
            $where[] = ['user_id', '=', $param['user_id']];
        }
        $or[] = ["case_id",'=',0];
        $or[] = ["user_id",'=',$param['user_id']];
        $list = $this->where($where)->orWhere($or)->orderByDesc("ins_times")->orderByDesc("created_at")->get();
        if (!empty($list)) {
            $list = $list->toArray();
            $url =  new Url();
            foreach ($list as $k => $v) {
                if (!empty($v['image'])) {
                    $list[$k]['image'] = $url::getHttpHost() . 'upload/' . $v['image'];
                }
                if (!empty($v['voice'])) {
                    $list[$k]['voice'] = $url::getHttpHost() . 'upload/' . $v['voice'];
                }
            }
//            $list = self::array_group_by($list, 'ins_times');
            return $list;
        }
        return [];
    }

    /***
     * @param $list
     * @return array
     * 分组排序
     */
    public static function array_group_by($list)
    {
        $newArray = array();
        foreach ($list as $v) {
            $newArray[$v['ins_times']][] = $v;
        }
        return $newArray;
    }

    /***
     * @param $data
     * @return int
     * 删除备忘录
     */
    public function delMemos($data)
    {
        return $this::destroy($data['id']);
    }

    /***
     * @param $data
     * 备忘录详情
     */
    public function detailMemos($data)
    {
        $res = $this->getInfo($data['id']);
        if (!empty($res->image)) {
            $res->image = Url::getHttpHost() . 'upload/' . $res->image;
        }
        if (!empty($res->voice)) {
            $res->voice = Url::getHttpHost() . 'upload/' . $res->voice;
        }
        return $res;
    }
    
}
